*==========================================================================================================================================================
*
*	Local Projections
*
*==========================================================================================================================================================


set more off

qui {


*-------------------------------------------------------------------------------------------------------------
*	SETTINGS
*-------------------------------------------------------------------------------------------------------------

* Variables
local vars "log_R_cad_er_hp log_R_sev_er_hp log_R_med_er_hp log_R_mad_er_hp log_R_bar_er_hp log_R_sar_er_hp log_R_lon_er_hp log_R_ams_er_hp log_R_par_er_hp log_R_ant_er_hp log_R_gen_er_hp log_R_mil_er_hp log_R_ven_er_hp log_R_ham_er_hp log_R_fra_er_hp "  
local lava ""Cadiz" "Sevilla" "Medina del Campo" "Madrid" "Barcelona" "Saragossa" "London" "Amsterdam" "Paris" "Antwerp" "Genoa" "Milan" "Venice" "Hamburg" "Frankfurt"" 

* irf horizon
local H 5
local F 5

* lags
local L 2
local LS `L'

*-------------------------------------------------------------------------------------------------------------

local count = 0
foreach v of local vars {
local count = `count'+1
n di "Variable `v'"

*_________ GET DATA ____________
use "data/working files/prepared.dta", clear

n tab year if disaster==1 & `v'!=.


*____ DECLARE TIME SERIES _____
gen id =1
tsset id year, yearly

*_________________ LOCAL PROJECTIONS _________________

***_________ CONTROLS
local indep	" gdp_r_adj_gr cpi_sil_gr wage_sil_gr itsN_gr" 

forvalues i=0/`L'{
	local indep_`i' = subinstr("`indep'"," "," l`i'_",.)
	}
	
local rhs ""
forvalues i = 1/`L'{
	local rhs "`rhs'`indep_`i''"
	}
	
	local not l0_`v'_gr l0_itsN_gr 
	local rhs: list rhs- not
	
	


	***_________ IMPULSE RESPONSE and CONFIDENCE INTERVAL variables
	gen irf_`v' = . if _n<=`H'+1
	
	gen se_`v' = . if _n<=`H'+1

	gen ci_up_`v' = . if _n<=`H'+1
	gen ci_lo_`v' = . if _n<=`H'+1
	gen ci_up90_`v' = . if _n<=`H'+1
	gen ci_lo90_`v' = . if _n<=`H'+1
	
	
	***_________ ESTIMATE COEFFICIENTS
	forvalues y = 0/`H' {
		n display "projection `y'"


			newey `v'_gr`y' l(1/`LS').`v'_gr l(-`F'/`LS').perclossN_vellon l(-`F'/`LS').perclossN l(-`F'/`LS').percloss_salvN `rhs' l(-`F'/`LS').percloss_pirN l(0/`LS').temperature l(0/`LS').warbeu l(0/`LS').warbciv, lag(5) force 

		* store IMPULSE RESPONSES and STANDARD ERRORS	
		lincom perclossN 
		replace irf_`v' = r(estimate) if _n==`y'+1 /* coefficients */
		replace se_`v' = r(se) if _n==`y'+1 /* standard errors */
		
		//restore
		}			


	***_________ CONFIDENCE INTERVALS
	replace ci_up_`v' = irf_`v' + 1*se_`v' if _n <= `H'+1
	replace ci_lo_`v' = irf_`v' - 1*se_`v' if _n <= `H'+1
	replace ci_up90_`v' = irf_`v' + 1.64*se_`v' if _n <= `H'+1
	replace ci_lo90_`v' = irf_`v' - 1.64*se_`v' if _n <= `H'+1

* plot prerequisites
gen time = _n-1 if(_n<=`H'+1)
drop if time > `H'
keep time irf_* ci_up_* ci_lo_* ci_up90_* ci_lo90_* se_*
order irf_* ci_up_* ci_lo_* time ci_up90_* ci_lo90_*
drop if time == . 
gen zero = 0 if time!=.

local title : word `count' of `lava'
local lsize "medlarge"
local tsize "medlarge"

if (`"`v'"'=="log_R_par_er_hp")  { //& `"`v'"'!="gdp_r_adj"
twoway  (rarea ci_lo90_`v' ci_up90_`v' time, fcolor(gs15) lcolor(gs15)) ///
		(rarea ci_lo_`v' ci_up_`v' time, fcolor(gs12) lcolor(gs12)) ///
		(line irf_`v' time, lcolor(black) lpattern(solid) lwidth(medthick)) ///
		(line zero time, lcolor(black) lwidth(medium)), xtitle("", size(`lsize')) ytitle("", height(1) size(`lsize')) xlabel(,labsize(`lsize')) ylabel(, nogrid angle(0) labsize(`lsize')) ///
		legend(off) title(`title', size(`tsize')) ///
		graphregion(color(white)) plotregion(color(white)) name(g`count', replace) nodraw 
		}
else {	
twoway  (rarea ci_lo90_`v' ci_up90_`v' time, fcolor(gs15) lcolor(gs15)) ///
		(rarea ci_lo_`v' ci_up_`v' time, fcolor(gs12) lcolor(gs12)) ///
		(line irf_`v' time, lcolor(black) lpattern(solid) lwidth(medthick)) ///
		(line zero time, lcolor(black) lwidth(medium)), xtitle("", size(`lsize')) ytitle("", height(1) size(`lsize')) xlabel(,labsize(`lsize')) ylabel(-1.5(1.5)3, nogrid angle(0) labsize(`lsize')) ///
		legend(off) title(`title', size(`tsize')) ///
		graphregion(color(white)) plotregion(color(white)) name(g`count', replace) nodraw 
	}

} // vars

graph combine g1 g2 g3 g4 g5 g6 g7 g8 g9 g10 g11 g12 g13 g14 g15, name(LP_allrates, replace) cols(3) imargin(medsmall) plotregion(color(white)) graphregion(color(white)) 
graph display LP_allrates, ysize(8) xsize(6)
graph export "results/FigureB14.pdf", replace 



}


*_________________________________________

			
